package com.gitee.huanminabc.jcommon.model;

import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 标识对象, 避免全项目重复定义(一般用于前端下拉菜单和搜索提示等场景)
 *  支持使用@DataIdName 注解标准目标的对象 ,这样就能利用下面的方法进行快速转换
 */
@Data
public class DataIdNameVO implements Serializable {
    /**
     * 数据的id
     */
    private Long id;
    /**
     * 数据的name
     */
    private String name;
    public DataIdNameVO() {

    }
    public DataIdNameVO(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public static DataIdNameVO build(Long id, String name) {
        return new DataIdNameVO(id, name);
    }

    public static <POJO> List<DataIdNameVO> buildList(List<POJO> list, Function<POJO, Long> id, Function<POJO, String> name) {
        return list.stream()
                .map(obj -> build(id.apply(obj), name.apply(obj)))
                .collect(Collectors.toList());
    }



}
